package com.ec50n9.server.module.common

import org.springframework.beans.BeanUtils

abstract class Render<E> {
    fun getSelf() = this as E
}

fun <S, T : Rendered<S>> Render<S>.renderTo(clazz: Class<T>): T {
    val temp = BeanUtils.instantiateClass(clazz)
    temp.renderFrom(getSelf())
    return temp
}

fun <S, T : Rendered<S>> Iterable<Render<S>>.renderTo(clazz: Class<T>): List<T> =
    map { render -> render.renderTo(clazz) }

interface Rendered<E> {
    fun renderFrom(from: E)
}

interface CreateForm<T : Render<*>> {
    fun renderTo(): T
}

interface UpdateForm<T : Render<*>> {
    fun renderTo(target: T)
}
